/*
 * standalone.c - minimal bootstrap for C library
 * Copyright (C) 2000 ARM Limited.
 * All rights reserved.
 */

/*
 * RCS $Revision: 2 $
 * Checkin $Date: 15/05/18 2:47p $ 0
 * Revising $Author: Hpchen0 $
 */

/*
 * This code defines a run-time environment for the C library.
 * Without this, the C startup code will attempt to use semi-hosting
 * calls to get environment information.
 */

#include <stdio.h>
#include "sys_uart.h"
#include "sys_owa.h"
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"

extern unsigned int Image$$RW_RAM1$$ZI$$Limit;


void _sys_exit(int return_code)
{
label:
	goto label; /* endless loop */
}

void _ttywrch(int ch)
{
	char tempch = (char)ch;
	(void)tempch;
}


/// @cond HIDDEN_SYMBOLS

__value_in_regs struct R0_R3 {
	unsigned heap_base, stack_base, heap_limit, stack_limit;
}
__user_setup_stackheap(unsigned int R0, unsigned int SP, unsigned int R2, unsigned int SL)

{
	struct R0_R3 config;


	config.heap_base = (unsigned int)&Image$$RW_RAM1$$ZI$$Limit;
	config.stack_base = SP;


	return config;
}
/// @endcond HIDDEN_SYMBOLS

int ttt;

int xtoi(char *s1)
{
	int ret;
	
	for(ret=0;s1&&*s1;s1++) 
	{
		if(*s1<='9')
			ret = *s1-'0'+(ret<<4); // val = val*16+int(*s1)
		else
			ret = 10+(*s1&0x5f)-'A'+(ret<<4); // val = val*16+int(toupper(*s1))
	}
	
	return ret;
}

int strncmp(const char *s1,const char *s2,size_t len)
{
	while(--len && *s1 && *s2 && (*s1==*s2)) s1++, s2++;
	
	return (*s1-*s2);
}

int strcmp(const char *s1, const char *s2)
{
	return strncmp(s1,s2,-1);
}

size_t strlen(const char *s1)
{
	int len;
	
	for(len=0;s1&&*s1++;len++);

	return len;
}

char *strtok(char *str, const char *dptr)
{
	static char *nxt = 0;

	int dlen = strlen(dptr);
	char *tmp;

		 if(str) tmp=str;
	else if(nxt) tmp=nxt;
	else return 0;
	
	char *ret=tmp;

	while(*tmp)
	{
		if(strncmp(tmp,dptr,dlen)==0)
		{
			*tmp=0;
			nxt = tmp+1;
			return ret;
		}
		tmp++;
	}
	nxt = 0;
	return ret;
}

//==============================================================================

int sys_aux_audio_open(void)
{
	owa_aux_open();
	return 0;
}

int sys_aux_audio_write(const short* buf, int sample_count)
{
	return owa_aux_write(buf, sample_count);
//	return sample_count;
}

int sys_aux_audio_drain(void)
{
	return 0;
}

int sys_aux_audio_close(void)
{
	owa_aux_close();
	return 0;
}

//------------------------------------------------------------------------------

int sys_event_create()
{
	SemaphoreHandle_t xSemaphore;

	xSemaphore = xSemaphoreCreateBinary();

	return (int)xSemaphore;
}

void sys_event_reset(int h)
{
}

void sys_event_set(int h)
{
	xSemaphoreGive((SemaphoreHandle_t)h);
}

void sys_event_wait(int h)
{
	xSemaphoreTake((SemaphoreHandle_t)h, portMAX_DELAY);
}

void sys_event_delete(int h)
{
	vSemaphoreDelete((SemaphoreHandle_t)h);
}

//------------------------------------------------------------------------------

void sys_sleep(int ms)
{
	vTaskDelay(ms / portTICK_PERIOD_MS);
}

int sys_thread_start(void* proc)
{
	TaskHandle_t xHandle = 0;

	xTaskCreate((TaskFunction_t)proc, "s_task", 2048, (void*)0, 5, &xHandle);

	return (int)xHandle;
}

void sys_thread_end(int h)
{
	vTaskDelete((TaskHandle_t)h);
}

//==============================================================================

/* end of file standalone.c */
/*AT2367791605016262732*/
